package com.thirteen.app.controller;


import com.thirteen.app.entity.Answer;
import com.thirteen.app.entity.QuestionRepository;
import com.thirteen.app.service.QuestionRepositoryService;
import com.thirteen.app.service.QuestionService;
import com.thirteen.app.utils.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 题库(Questionrepository)表控制层
 *
 * @author makejava
 * @since 2021-09-06 10:38:20
 */
@RestController
@EnableOpenApi
@RequestMapping("/api")
@Api("题库管理模块")
@CrossOrigin
public class QuestionRepositoryController {
    /**
     * 服务对象
     */
    @Resource
    private QuestionRepositoryService questionRepositoryService;

    @Resource
    private QuestionService questionService;


    @ApiOperation(value = "添加题库")
    @PostMapping("/teacher/qsBank/createQsBank")
    public CommonResult add(String title, String remark) {
        QuestionRepository questionRepository = new QuestionRepository();
        System.out.println(title);
        System.out.println(remark);
        questionRepository.setTitle(title);
        questionRepository.setRemark(remark);
        return CommonResult.success(questionRepositoryService.insert(questionRepository));
    }

    @ApiOperation(value = "向指定题库增加题目")
    @PostMapping("/teacher/qsBank/createQu")
    public CommonResult createQu(String content,
                                 String courseName,
                                 Integer repositoryId,
                                 Integer questionType,
                                 Integer answerCount,
                                 String analysis,
                                 String solution,//答案
                                 String[] tagList,
                                 String[] contentList) {
        List<Answer> list = new ArrayList<>();
        if (tagList == null || contentList == null) {    //填空判断简答的这两是空的
            if (questionType == 4 || questionType == 5) {   //填空简答在Answer表只有一条记录
                list.add(new Answer());
            } else {                                   //判断在数据库的Answer表里需要插入两条记录
                list.add(new Answer());
                list.add(new Answer());
            }
        }

        if (tagList != null && contentList != null) {     //单选和多选
            for (int i = 0; i < tagList.length; i++) {
                Answer answer = new Answer();
                answer.setTag(tagList[i]);   //设置Answer的Tag
                answer.setContent(contentList[i]);//设置Answer的content
                list.add(answer);
            }
        }

        return CommonResult.success(questionRepositoryService.createQu(content, analysis, courseName, repositoryId, questionType, answerCount, solution, list));
    }

    @GetMapping("/teacher/qsBank/selectQuByType")
    @ApiOperation("根据题库id和类型列表查询题目")
    public CommonResult selectQuByType(Integer qsBankId, Integer[] questionType) {
        return CommonResult.success(questionRepositoryService.selectQuByType(qsBankId, questionType));
    }

    @ApiOperation(value = "删除指定题库的指定题目")
    @PostMapping("/teacher/qsBank/deleteQu")
    public CommonResult deleteQu(Integer questionId, Integer repositoryId) {
        return CommonResult.success(questionRepositoryService.updateRepositoryQuestion(questionId, repositoryId));
    }


    @GetMapping("/teacher/qsBank/selectQuByQsBankId")
    @ApiOperation("获取指定题库的所有题目")
    public CommonResult selectQuByQsBankId(QuestionRepository questionRepository) {
        return CommonResult.success(questionService.getQuestionList(questionRepository.getRepositoryId(), null, null, null));
    }


    @GetMapping("/student/qsBank/selectAllQsBank")
    @ApiOperation("根据题库名称模糊查询所有题库")
    public CommonResult selectAllQsBankStudent(String title) {
        return CommonResult.success(questionRepositoryService.selectAllQsBank(title));
    }

    @GetMapping("/teacher/qsBank/selectAllQsBank")
    @ApiOperation("根据题库名称模糊查询所有题库")
    public CommonResult selectAllQsBankTeacher(String title) {
        return CommonResult.success(questionRepositoryService.selectAllQsBank(title));
    }


    @GetMapping("/teacher/qsBank/updateQuestion")
    @ApiOperation("修改题目以及题目选项")
    public int updateQuestion(String content,
                              String analysis,
                              Integer questionId,
                              Integer questionType,
                              Integer answerCount,
                              String solution,
                              String[] tagList,
                              String[] contentList) {
        List<Answer> list = new ArrayList<>();
        if (tagList == null || contentList == null) {    //填空判断简答的这两是空的
            if (questionType == 4 || questionType == 5) {   //填空简答在Answer表只有一条记录
                list.add(new Answer());
            } else {                                   //判断在数据库的Answer表里需要插入两条记录
                list.add(new Answer());
                list.add(new Answer());
            }
        }

        if (tagList != null && contentList != null) {     //单选和多选
            for (int i = 0; i < tagList.length; i++) {
                Answer answer = new Answer();
                answer.setTag(tagList[i]);   //设置Answer的Tag
                answer.setContent(contentList[i]);//设置Answer的content
                list.add(answer);
            }
        }

        return questionRepositoryService.updateQuestion(content, questionId, answerCount, questionType, analysis, solution, list);
    }


}
